package com.dubito.dose.data

import androidx.room.TypeConverter
import com.dubito.dose.utils.TimeOfDay
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import java.util.Collections
import java.util.Date

/**
 * author: LiLiang
 * createTime: 2022/9/5-13:11
 * remark:
 **/
class Converters {

    val gson = Gson()

    @TypeConverter
    fun fromTimestamp(value: Long?): Date? {
        return value?.let { Date(it) }
    }

    @TypeConverter
    fun dateToTimestamp(date: Date?): Long? {
        return date?.time
    }

    @TypeConverter
    fun stringToTimeOfDay(data: String?): List<TimeOfDay?>? {
        if (data == null) {
            return Collections.emptyList()
        }
        val listType = object : TypeToken<List<TimeOfDay?>?>() {}.type
        return gson.fromJson<List<TimeOfDay?>>(data, listType)
    }

    @TypeConverter
    fun someObjectListToString(someObjects: List<TimeOfDay?>?): String? {
        return gson.toJson(someObjects)
    }

}